home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / DATA_MG.ZIP / L.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-30  |  3.6 KB  |  163 lines

  1. /*
  2. **  L.C - A Simple Linked List manager.  This file contains the functions
  3. **        to Create, insert, find and delete nodes in a linked list.
  4. **        It is for academic purposes only, and is not intended as an
  5. **        efficient and/or professionally useable list manager.
  6. **
  7. **        These functions have the ability to main linked lists of chars, ints,
  8. **        longs, and character strings.  The ListNode function allows a List
  9. **        to be dumped to the screen.  The lists are maintain in correct order,
  10. **        according to the data type of the list.  The lists are dynamic.
  11. **
  12. ** To compile:  TCC or QCL  -c L.C
  13. **
  14. ** Mario Giannini
  15. */
  16. #include <stdio.h>
  17. #include <malloc.h>  /* alloc.h for Turbo C */
  18. #include <string.h>
  19. #include "l.h"
  20.  
  21. /*
  22. ** This array of pointers to functions is described later on 
  23. */
  24. int (*Compares[4])()={IntCmp, IntCmp, LongCmp, StringCmp};
  25.  
  26. Node *MakeList(int DataType)  /* Starts a list in memory */
  27. {
  28.     Node *P;
  29.  
  30.     if(!(P=calloc(sizeof(Node),1)))
  31.         return((Node *)NULL);
  32.     P->DataType=DataType;
  33.     return(P);
  34. }
  35.  
  36. int AddNode(Node *List, void *Data2Add) /* Add Node into list, in correct order */
  37. {
  38.     Node *P, *Q, *S;
  39.  
  40.     P=List;
  41.     if(!(S=calloc(sizeof(Node),1)))
  42.         return(-1);
  43.     S->DataType=List->DataType;
  44.     switch (S->DataType) {
  45.         case CHAR:
  46.             S->Data.Char=*(char *)Data2Add;
  47.             break;
  48.         case INT:
  49.             S->Data.Int=*(int *)Data2Add;
  50.             break;
  51.         case LONG:
  52.             S->Data.Long=*(long *)Data2Add;
  53.             break;
  54.         case CHARSTR:
  55.             if( (S->Data.CPntr=strdup(Data2Add))==NULL)
  56.             {
  57.                 free(S);
  58.                 return(-1);
  59.             }
  60.             break;
  61.     }
  62. /*
  63. ** This line may need some explanation.  Because of the possible data types
  64. ** to be maintained in the list, several Compare() functions are needed. To
  65. ** make life easier (?)  The functions that do the compare have been setup
  66. ** into an array of pointers to functions, each functions position in the
  67. ** array is equivalent to its type, so that the function Compare[INT]() will
  68. ** compare integers, and Compare[LONG]() would compare longs, etc.
  69. */
  70.     while( P && Compares[List->DataType](P, Data2Add)<1)
  71.     {
  72.         Q=P;
  73.         P=P->Next;
  74.     }
  75.     S->Next=P;
  76.     S->Prev=Q;
  77.     Q->Next=S;
  78.     if(P)
  79.         P->Prev=S;
  80.     if(!S->Next)
  81.         List->Prev=S;
  82.     return(0);
  83. }
  84.  
  85. /* Find Exact match in List, returns pointer to node if found, NULL if not */
  86. Node *FindNode(Node *P, void *Data2Find) 
  87. {
  88.     Node *R=P;
  89.     while( P && Compares[R->DataType](P, Data2Find)<0)
  90.         P=P->Next;
  91.     if(Compares[R->DataType](P, Data2Find)==0)
  92.         return(P);
  93.     return(NULL);
  94. }
  95.  
  96. DestroyList(Node *P) /* Remove an entire list from memory */
  97. {
  98.     Node *Q;
  99.  
  100.     do {
  101.         Q=P->Next;
  102.         free(P);
  103.         P=Q;
  104.     } while(P);
  105. }
  106.  
  107. void DeleteNode(Node *P) /* Remove a single Node from memory */
  108. {
  109.     if(P->Prev)
  110.         if(P->Next)
  111.             P->Prev->Next=P->Next;
  112.         else
  113.             P->Prev->Next=NULL;
  114.     if(P->Next)
  115.         if(P->Prev)
  116.             P->Next->Prev=P->Prev;
  117.         else
  118.             P->Next->Prev=NULL;
  119.     if(P->DataType==CHARSTR)
  120.         free(P->Data.CPntr);
  121.     free(P);
  122. }
  123.  
  124. void ListNodes(Node *P, int Dir) /* List contents of node in memory */
  125. {
  126.     Node *Q=P;
  127.  
  128.     if(Dir)
  129.         P=P->Prev;
  130.     else
  131.         P=P->Next;
  132.     while(P && P!=Q)
  133.     {
  134.         if(P->DataType==INT)
  135.             printf("%d  ", P->Data);
  136.         else
  137.             printf(" [%s]  ", P->Data);
  138.         if(Dir)
  139.             P=P->Prev;
  140.         else
  141.             P=P->Next;
  142.     }
  143. }
  144. /*
  145. **  Some generic compare functions used by the functions above.
  146. */
  147.  
  148. IntCmp(Node *N, int *D2)  /* Compare integers (and chars) */
  149. {
  150.     return(N->Data.Int-*D2);
  151. }
  152.  
  153. int LongCmp(Node *N, long *D2)  /* Compare longs */
  154. {
  155.     return((int)(N->Data.Long-*D2));
  156. }
  157.  
  158. int StringCmp(Node *N, char *Str) /* Compare strings */
  159. {
  160.     return(stricmp(N->Data.CPntr, Str));
  161. }
  162.  
  163.